"use strict";
// object
// 声明对象类型
// 限制person对象的具体内容，使⽤【,】分隔，问号代表可选属性
let person;
//  限制car对象的具体内容，使⽤【;】分隔，必须有price和color属性，其他属性不去限制，有没有都⾏
let car;
// 限制student对象的具体内容，使⽤【回⻋】分隔
let student;
// 以下代码均⽆警告
person = { name: '张三', age: 18 };
person = { name: '李四' };
car = { price: 100, color: '红⾊' };
student = { id: 'tetqw76te01', grade: 3 };
// 声明函数类型
let demo; // =>为函数类型声明
demo = function (x, y) {
    return x + y;
}; // 匿名函数
console.log(demo(1, 4)); // 调用函数
// 数组类型声明
let arr1; // 该⾏代码等价于： let arr1: Array<string>
let arr2; // 该⾏代码等价于： let arr2: Array<number>
arr1 = ["a", "b", "c"];
arr2 = [1, 3, 5, 7, 9];
// tuple 一个长度固定的数组
let t;
t = ["hello", 123];
// 警告，不能将类型“[string, number, boolean]”分配给类型“[string, number]”
// t = ["hello", 123, false];
// enum
// 定义⼀个枚举
var Color;
(function (Color) {
    Color[Color["Red"] = 0] = "Red";
    Color[Color["Blue"] = 1] = "Blue";
    Color[Color["Black"] = 2] = "Black";
    Color[Color["Gold"] = 3] = "Gold";
})(Color || (Color = {}));
// 定义⼀个枚举，并指定其初识数值
var Color2;
(function (Color2) {
    Color2[Color2["Red"] = 6] = "Red";
    Color2[Color2["Blue"] = 7] = "Blue";
    Color2[Color2["Black"] = 8] = "Black";
    Color2[Color2["Gold"] = 9] = "Gold";
})(Color2 || (Color2 = {}));
console.log(Color);
/*
 {
 0: 'Red',
 1: 'Blue',
 2: 'Black',
 3: 'Gold',
 Red: 0,
 Blue: 1,
 Black: 2,
 Gold: 3
 }
*/
console.log(Color2);
/*
 {
 6: 'Red',
 7: 'Blue',
 8: 'Black',
 9: 'Gold',
 Red: 6,
 Blue: 7,
 Black: 8,
 Gold: 9
 }
*/
// 定义⼀个phone变量，并设置对⻬进⾏限制
let phone;
phone = { name: '华为Mate60', price: 6500, color: Color.Red };
console.log(phone.name);
// phone = { name: "iPhone15Pro", price: 7999, color: Color.Blue };
if (phone.color === Color.Red) {
    console.log("⼿机是红⾊的");
}
// type
// 性别的枚举
var Gender;
(function (Gender) {
    Gender[Gender["Male"] = 0] = "Male";
    Gender[Gender["Female"] = 1] = "Female";
})(Gender || (Gender = {}));
// 定义两个学⽣变量：s1、s2
let s1;
let s2;
s1 = { name: '张三', age: 18, gender: Gender.Male, grade: 1 };
s2 = { name: '李四', age: 18, gender: Gender.Female, grade: 2 };
const f1 = function () {
    return 219;
}; // 使用类型声明限制函数返回值为void时，TS并不会严格要求返回void

// class类
class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    speak() {
        console.log(`我叫${this.name},今年${this.age}岁`);
    }
}
const p1 = new Person('asd', 18);
console.log(p1);
p1.speak();
class Student1 extends Person {
    constructor(name, age, grade) {
        super(name, age); //super 关键字调用父类 Person 的构造函数
        this.grade = grade;
    }
    study() {
        console.log(`${this.name}正在学习`);
    }
    speak() {
        console.log(`我是学生，我叫${this.name},今年${this.age}岁`);
    } // 覆写
}
const stu1 = new Student1('qwe', 16, '高三');
stu1.study();
stu1.speak();
class Person1 {
    constructor(name, age, IDcard) {
        this.name = name;
        this.age = age;
        this.IDcard = IDcard;
    }
    getDetails() {
        return `我叫${this.name}`;
    } // 受保护，外部不可用
    getPrivateInfo() {
        return `我是${this.name},我的身份证号是${this.IDcard}`;
    }
    getInfo() {
        return `我叫${this.name},今年${this.age}岁`;
    }
    getFullInfo() {
        return this.getInfo() + ',' + this.getPrivateInfo();
    }
} // 简写形式
const p2 = new Person1('asd', 18, '12345678');
console.log(p2.getInfo());
console.log(p2.getFullInfo());
// console.log(p2.getPrivateInfo)私有属性不可访问
// public 类内部，子类，类外部都可以访问
// protected 类内部，子类可以访问
// private 类内部可以访问
// readonly 只读属性
// 抽象类
class Package {
    // 构造方法
    constructor(weight) {
        this.weight = weight;
    }
    // 具体方法
    printPackage() {
        console.log(`包裹重${this.weight}kg, 运⾏费${this.calculatePrice()}`); // 通用接口，基础实现
    }
}
class StandardPackage extends Package {
    constructor(weight, unitPrice) {
        super(weight);
        this.unitPrice = unitPrice;
    } // 调⽤⽗类的构造⽅法
    // 重写⽗类的抽象⽅法
    calculatePrice() {
        return this.weight * this.unitPrice; // 关键实现
    }
}
const sp = new StandardPackage(10, 20);
sp.printPackage();
class Person2 {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    speak(n) {
        console.log(`我叫${this.name},今年${this.age}岁,我会说${n}句话`);
    }
}
const p3 = new Person2('asd', 18);
p3.speak(5);
const a = 1;
const user = {
    name: '张三',
    gender: '男',
    run(n) {
        console.log(`跑了${n}圈`);
    }
};
user.run(12);
const count = function (a, b) {
    return a + b;
};
console.log(count(1, 2));
// 泛型
function test(a, b) {
    return b;
}
// 不指名类型，TS会⾃动推断出来
console.log(test(10, 20));
// 指名具体的类型
console.log(test(10, "hello"));
